package com.qj.daibei.controller;

import com.qj.daibei.pojo.DataFiling;
import com.qj.daibei.pojo.User;
import com.qj.daibei.service.AccountService;
import com.qj.daibei.service.DataFilingService;
import com.qj.daibei.service.LoanService;
import com.qj.daibei.service.UserService;
import com.qj.daibei.util.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * author kxz
 * createTime on 2021/10/15
 */
@Controller
public class AccountController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private DataFilingService dataFilingService;

    @Autowired
    private LoanService loanService;

    @Autowired
    private UserService userService;

    @RequestMapping("/queryUserById")
    @ResponseBody
    public User queryUserById(Integer updateId) {
        return accountService.queryUserById(updateId);
    }

    @RequestMapping("/updateUserById")
    @ResponseBody
    public String updateUserById(User user, @RequestParam(value = "file-headImg") MultipartFile multipartFile, HttpSession session) {
        String str = null;
        try {
            str = FileUpload.doSingleFileUpload(multipartFile, "avatar");
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setAvatar(str);
        System.out.println(user);
        accountService.updateUserById(user);
        User user1 = accountService.queryUserById(user.getUserId());
        session.setAttribute("loginUser",user1);
        return "ok";
    }

    @RequestMapping("/getResultById")
    @ResponseBody
    public int getResult(Integer updateId) {
        DataFiling dataFiling = dataFilingService.queryForRecordByUserId(updateId);
        return dataFiling.getResult();
    }
    @RequestMapping("/getMoneyById")
    @ResponseBody
    public int getMoney(Integer updateId) {
        int money=loanService.queryForRecordByUserId(updateId);
        return money;
    }
    @RequestMapping("/getOnlyMoney")
    @ResponseBody
    public int onlyMoney(Integer updateId){
        DataFiling dataFiling = dataFilingService.queryForRecordByUserId(updateId);
        System.out.println("===============================================================" + dataFiling);
        int money=loanService.queryForRecordByUserId(updateId);
        System.out.println("===============================================================" + money);
        System.out.println("===============================================================" + (dataFiling.getResult()-money));
        return dataFiling.getResult()-money;
    }
    @RequestMapping("/getIntegralById")
    @ResponseBody
    public User getIntegral(Integer updateId){
        User user=userService.queryForUserById(updateId);
        return user;
    }
}
